package com.skt.common_hr.congrest.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.congrest.CelebrationFacade;
	import com.wcl.dataloader.*;

	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MemberSearchListProxy extends CommonProxy
	{
		public static const NAME:String = "MemberSearchListProxy";

		public var orgList:XMLList;

		//사번
		public var pernr:String;

		//이름
		public var name:String;

		//부서명(상위조직명 포함)
		public var stext:String;

		//조회
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function MemberSearchListProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, auth:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_AUTH"] = auth;

			loadData("Z_HR_0015", param, result, fault);
		}




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				orgList = parseOrgList(event.result.RTAB);
				facade.sendNotification(CelebrationFacade.LOAD_ORGLIST_COMPLETE, this);
			}
			else {
				trace( "failed to get post list");
				loadFaultMessage = event.result["RETTEXT"]
				facade.sendNotification(CelebrationFacade.LOAD_ORGLIST_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			facade.sendNotification(CelebrationFacade.LOAD_ORGLIST_FAILED, this);
			loadFaultMessage = event.fault["RETTEXT"]
			CursorManager.removeBusyCursor();
		}





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			var root:XML = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
			prevArr[root.@level] = root;

			for(var i:int=1; i<retObj.length; i++)
			{
				var rec:Object = retObj[i];
				var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
				if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
					XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
				}
				prevArr[node.@level] = node;
			}
			return new XMLList(root);
		}

	}
}